home *** CD-ROM | disk | FTP | other *** search
/ Aminet 28 / Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso / Aminet / dev / lang / fpcsrc.lha / fpc / compiler / asmutils.pas < prev    next >
Pascal/Delphi Source File  |  1998-09-24  |  60KB  |  1,698 lines

  1. {
  2.     $Id: asmutils.pas,v 1.1.1.1 1998/03/25 11:18:12 root Exp $
  3.     Copyright (c) 1998 Carl Eric Codere
  4.  
  5.     This unit implements some support routines for assembler parsing
  6.  
  7.     This program is free software; you can redistribute it and/or modify
  8.     it under the terms of the GNU General Public License as published by
  9.     the Free Software Foundation; either version 2 of the License, or
  10.     (at your option) any later version.
  11.  
  12.     This program is distributed in the hope that it will be useful,
  13.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.     GNU General Public License for more details.
  16.  
  17.     You should have received a copy of the GNU General Public License
  18.     along with this program; if not, write to the Free Software
  19.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  
  21.  **********************************************************************}
  22.  
  23. Unit AsmUtils;
  24.  
  25. {*************************************************************************}
  26. {  This unit implements some objects as well as utilities which will be   }
  27. {  used by all inline assembler parsers (non-processor specific).         }
  28. {                                                                         }
  29. {  Main routines/objects herein:                                          }
  30. {  o Object TExprParse is a simple expression parser to resolve assembler }
  31. {    expressions. (Based generally on some code by Thai Tran from SWAG).  }
  32. {  o Object TInstruction is a simple object used for instructions         }
  33. {  o Record TOperand is a simple record used to store information on      }
  34. {    each operand.                                                        }
  35. {  o String conversion routines from octal,binary and hex to decimal.     }
  36. {  o A linked list object/record for local labels                         }
  37. {  o Routines for retrieving symbols (local and global)                   }
  38. {  o Object for a linked list of strings (with duplicate strings not      }
  39. {    allowed).                                                            }
  40. {  o Non-processor dependant routines for adding instructions to the      }
  41. {    instruction list.                                                    }
  42. {*************************************************************************}
  43.  
  44.  
  45. {--------------------------------------------------------------------}
  46. { LEFT TO DO:                                                        }
  47. { o Fix the remaining bugs in the expression parser, such as with    }
  48. {     4+-3                                                           }
  49. { o Add support for local typed constants search.                    }
  50. { o Add support for private/protected fields in method assembler     }
  51. {    routines.                                                       }
  52. {--------------------------------------------------------------------}
  53. Interface
  54.  
  55. Uses
  56.   symtable,aasm,hcodegen,verbose,systems,globals,files,strings,
  57.   cobjects,
  58. {$ifdef i386}
  59.   i386;
  60. {$endif}
  61. {$ifdef m68k}
  62.    m68k;
  63. {$endif}
  64.  
  65.  
  66. Const
  67.   RPNMax = 10;             { I think you only need 4, but just to be safe }
  68.   OpMax  = 25;
  69.  
  70.   maxoperands = 3;         { Maximum operands for assembler instructions }
  71.  
  72.  
  73. Type
  74.  
  75.  
  76.   {---------------------------------------------------------------------}
  77.   {                     Label Management types                          }
  78.   {---------------------------------------------------------------------}
  79.  
  80.  
  81.     PAsmLabel = ^TAsmLabel;
  82.     PString = ^String;
  83.  
  84.     { Each local label has this structure associated with it }
  85.     TAsmLabel = record
  86.       name: PString;    { pointer to a pascal string name of label }
  87.       lab: PLabel;      { pointer to a label as defined in FPC     }
  88.       emitted: boolean; { as the label itself been emitted ?       }
  89.       next: PAsmLabel;  { next node                                }
  90.     end;
  91.  
  92.     TAsmLabelList = Object
  93.     public
  94.       First: PAsmLabel;
  95.       Constructor Init;
  96.       Destructor Done;
  97.       Procedure Insert(s:string; lab: PLabel; emitted: boolean);
  98.       Function Search(const s: string): PAsmLabel;
  99.     private
  100.       Last: PAsmLabel;
  101.       Function NewPasStr(s:string): PString;
  102.     end;
  103.  
  104.  
  105.  
  106.   {---------------------------------------------------------------------}
  107.   {                 Instruction management types                        }
  108.   {---------------------------------------------------------------------}
  109.  
  110.   toperandtype = (OPR_NONE,OPR_REFERENCE,OPR_CONSTANT,OPR_REGISTER,OPR_LABINSTR,
  111.                   OPR_REGLIST);
  112.  
  113.     { When the TReference field isintvalue = TRUE }
  114.     { then offset points to an ABSOLUTE address   }
  115.     { otherwise isintvalue should always be false }
  116.  
  117.     { Special cases:                              }
  118.     {   For the M68k Target, size is UNUSED, the  }
  119.     {   opcode determines the size of the         }
  120.     {   instruction.                              }
  121.     {  DIVS/DIVU/MULS/MULU of the form dn,dn:dn   }
  122.     {  is stored as three operands!!              }
  123.  
  124.  
  125.     { Each instruction operand can be of this type }
  126.     TOperand = record
  127.       size: topsize;
  128.       opinfo: longint; { ao_xxxx flags }
  129.       case operandtype:toperandtype of
  130.        { the size of the opr_none field should be at least equal to each }
  131.        { other field as to facilitate initialization.                    }
  132.        OPR_NONE: (l: array[1..sizeof(treference)] of byte);
  133.        OPR_REFERENCE: (ref:treference);
  134.        OPR_CONSTANT:  (val: longint);
  135.        OPR_REGISTER:  (reg:tregister);
  136.        OPR_LABINSTR: (hl: plabel);
  137.        { Register list such as in the movem instruction }
  138.        OPR_REGLIST:  (list: set of tregister);
  139.     end;
  140.  
  141.  
  142.  
  143.     TInstruction = object
  144.     public
  145.       operands: array[1..maxoperands] of TOperand;
  146.       { if numops = zero, a size may still be valid in operands[1] }
  147.       { it still should be checked.                                }
  148.       numops: byte;
  149.       { set to TRUE if the instruction is labeled.                }
  150.       labeled: boolean;
  151.       { This is used for instructions such A_CMPSB... etc, to determine }
  152.       { the size of the instruction.                                    }
  153.       stropsize: topsize;
  154.       procedure init;
  155.       { sets up the prefix field with the instruction pointed to in s }
  156.       procedure addprefix(tok: tasmop);
  157.       { sets up the instruction with the instruction pointed to in s }
  158.       procedure addinstr(tok: tasmop);
  159.       { get the current instruction of this object }
  160.       function getinstruction: tasmop;
  161.       { get the current prefix of this instruction }
  162.       function getprefix: tasmop;
  163.     private
  164.       prefix: tasmop;
  165.       instruction: tasmop;
  166.     end;
  167.  
  168.  
  169.  
  170.  
  171.   {---------------------------------------------------------------------}
  172.   {                   Expression parser types                           }
  173.   {---------------------------------------------------------------------}
  174.  
  175.   { expression parser error codes }
  176.   texpr_error =
  177.   (zero_divide,       { divide by zero.     }
  178.    stack_overflow,    { stack overflow.     }
  179.    stack_underflow,   { stack underflow.    }
  180.    invalid_number,    { invalid conversion  }
  181.    invalid_op);       { invalid operator    }
  182.  
  183.  
  184.    TExprOperator = record
  185.     ch: char;           { operator }
  186.     is_prefix: boolean; { was it a prefix, possible prefixes are +,- and not }
  187.    end;
  188.  
  189.   String15 = String[15];
  190.   {**********************************************************************}
  191.   { The following operators are supported:                              }
  192.   {  '+' : addition                                                     }
  193.   {  '-' : subtraction                                                  }
  194.   {  '*' : multiplication                                               }
  195.   {  '/' : modulo division                                              }
  196.   {  '^' : exclusive or                                                 }
  197.   {  '<' : shift left                                                   }
  198.   {  '>' : shift right                                                  }
  199.   {  '&' : bitwise and                                                  }
  200.   {  '|' : bitwise or                                                   }
  201.   {  '~'